﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XAJH
{
    /// <summary>
    /// 窗口帮助类
    /// </summary>
    public class FormHelper
    {
        /// <summary>
        /// 获得包含指定点的窗口的句柄。
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "WindowFromPoint")]
        internal extern static IntPtr WindowFromPoint(System.Drawing.Point point);

        /// <summary>
        /// 返回父窗口中包含了指定点的第一个子窗口的句柄。
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="xPoint"></param>
        /// <param name="yPoint"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ChildWindowFromPoint")]
        internal extern static IntPtr ChildWindowFromPoint(IntPtr hWnd, long xPoint, long yPoint);

        /// <summary>
        /// 获取一个前台窗口的句柄（窗口与用户当前的工作）
        /// </summary>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
        public static extern IntPtr GetForegroundWindow();

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetActiveWindow")]
        public static extern IntPtr GetActiveWindow();

        /// <summary>
        /// 获取桌面窗口的句柄。
        /// </summary>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        public static extern IntPtr GetDesktopWindow();

        /// <summary>
        /// 获取窗口客户区的坐标
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="lpRect"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetClientRect")]
        internal extern static bool GetClientRect(IntPtr hWnd, out System.Drawing.Rectangle lpRect);

        #region GetWindowRect
        
        /// <summary>
        /// 获取指定窗口的边框矩形的尺寸。(该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。)
        /// </summary>
        /// <param name="hWnd"> 窗口句柄 </param>
        /// <param name="lpRect"> 矩形结构 </param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetWindowRect")]
        private extern static bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        /// <summary>
        /// 获取窗口矩形区域
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public static System.Drawing.Rectangle GetWindowRect(IntPtr hWnd)
        {
            RECT rect;
            GetWindowRect(hWnd, out rect);
            return new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
        }

        #endregion



        /// <summary>
        /// 激活一个窗口
        /// </summary>
        /// <param name="hWnd"></param>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetActiveWindow")]
        public extern static void SetActiveWindow(IntPtr hWnd);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "IsWindowEnabled")]
        public extern static bool IsWindowEnabled(IntPtr hWnd);


    }
}
